package com.ruoyi.receipt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.receipt.domain.TbKnowledgePlan;
import com.ruoyi.receipt.service.ITbKnowledgePlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.receipt.domain.TbKnowledgeManager;
import com.ruoyi.receipt.service.ITbKnowledgeManagerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 知识点管理Controller
 * 
 * @author ruoyi
 * @date 2021-03-29
 */
@RestController
@RequestMapping("/receipt/knowledgeManager")
public class TbKnowledgeManagerController extends BaseController
{
    @Autowired
    private ITbKnowledgeManagerService tbKnowledgeManagerService;

    @Autowired
    private ITbKnowledgePlanService tbKnowledgePlanService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询知识点管理列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeManager:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbKnowledgeManager tbKnowledgeManager)
    {
        startPage();
        List<TbKnowledgeManager> list = tbKnowledgeManagerService.selectTbKnowledgeManagerList(tbKnowledgeManager);
        return getDataTable(list);
    }

    /**
     * 导出知识点管理列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeManager:export')")
    @Log(title = "知识点管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbKnowledgeManager tbKnowledgeManager)
    {
        List<TbKnowledgeManager> list = tbKnowledgeManagerService.selectTbKnowledgeManagerList(tbKnowledgeManager);
        ExcelUtil<TbKnowledgeManager> util = new ExcelUtil<TbKnowledgeManager>(TbKnowledgeManager.class);
        return util.exportExcel(list, "manager");
    }

    /**
     * 导出知识点审核列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeManagerAudit:export')")
    @Log(title = "知识点管理", businessType = BusinessType.EXPORT)
    @GetMapping("/exportAudit")
    public AjaxResult exportAudit(TbKnowledgeManager tbKnowledgeManager)
    {
        List<TbKnowledgeManager> list = tbKnowledgeManagerService.selectTbKnowledgeManagerList(tbKnowledgeManager);
        ExcelUtil<TbKnowledgeManager> util = new ExcelUtil<TbKnowledgeManager>(TbKnowledgeManager.class);
        return util.exportExcel(list, "manager");
    }

    /**
     * 获取知识点管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeManager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbKnowledgeManagerService.selectTbKnowledgeManagerById(id));
    }

    /**
     * 新增知识点管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeManager:add')")
    @Log(title = "知识点管理", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody TbKnowledgeManager tbKnowledgeManager)
    {
        TbKnowledgePlan tbKnowledgePlan = new TbKnowledgePlan();
        tbKnowledgeManager.setId(idWorker.nextId());
        //这里保存一条知识点信息 一条知识点教案
        tbKnowledgePlan.setContent(tbKnowledgeManager.getTeachingPlanContent());
        tbKnowledgePlan.setKnowledgeManagerId(tbKnowledgeManager.getId());
        tbKnowledgePlan.setId(idWorker.nextId());
        tbKnowledgePlanService.insertTbKnowledgePlan(tbKnowledgePlan);
        return toAjax(tbKnowledgeManagerService.insertTbKnowledgeManager(tbKnowledgeManager));
    }

    /**
     * 修改知识点管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeManager:edit')")
    @Log(title = "知识点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody TbKnowledgeManager tbKnowledgeManager)
    {
        //修改知识点教案内容
        Map<String,Object> map = new HashMap<>();
        map.put("manageId",tbKnowledgeManager.getId());
        map.put("content",tbKnowledgeManager.getTeachingPlanContent());
        tbKnowledgePlanService.updateTbKnowledgePlanContent(map);
        return toAjax(tbKnowledgeManagerService.updateTbKnowledgeManager(tbKnowledgeManager));
    }

    /**
     * 删除知识点管理
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeManager:remove')")
    @Log(title = "知识点管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbKnowledgeManagerService.deleteTbKnowledgeManagerByIds(ids));
    }

    /**
     * 逻辑删除知识点
     */
    @PreAuthorize("@ss.hasPermi('receipt:knowledgeManager:remove')")
    @Log(title = "知识点管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/delManagerLogic/{ids}")
    public AjaxResult delManagerLogic(@PathVariable Long[] ids)
    {
        return toAjax(tbKnowledgeManagerService.delManagerLogic(ids));
    }


    @GetMapping("/listName/{name}")
    public List<TbKnowledgeManager> listName(@PathVariable String name){
        List<TbKnowledgeManager> tbKnowledgeManagers = tbKnowledgeManagerService.selectKmName(name);
        return tbKnowledgeManagers;
    }

    /**
     * 查询审核知识点
     * @param tbKnowledgeManager
     * @return
     */
    @GetMapping("/selectKnowledge")
    public TableDataInfo selectKnowledge(TbKnowledgeManager tbKnowledgeManager){
        startPage();
        List<TbKnowledgeManager> tbKnowledgeManagers = tbKnowledgeManagerService.selectKonwledgeStatus(tbKnowledgeManager);
        return getDataTable(tbKnowledgeManagers);
    }
}
